{
  Copyright 2012 Sergey Ostanin

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
}

unit ChoiceEditor;

interface

uses
  Windows, Types, Classes, SysUtils, FileUtil, Forms, Controls, Graphics,
  Dialogs, StdCtrls, ExtCtrls, Shape, superobject, PadEditor, VisualUtils,
  MiscUtils;

type
  TChoiceEditorFrame = class(TShapeFrame)
    cbCorrect: TCheckBox;
    imgFixed: TImage;
    imgNegative: TImage;
    lblSpacer: TLabel;
    pnlFlags: TPanel;
    pnlLeft: TPanel;
    procedure cbCorrectClick(Sender: TObject);
    procedure cbCorrectEnter(Sender: TObject);
  private
    FPadEditor: TPadEditorFrame;
    { private declarations }
  public
    procedure AfterConstruction; override;
    destructor Destroy; override;
    procedure Render;
    function Fixed: Boolean;
    procedure SetFixed(Value: Boolean);
    { public declarations }
  end;
  TChoiceEditorFrameList = TGenericObjectList<TChoiceEditorFrame>;

implementation

{$R *.lfm}
{$R *.rc}

const
  CORRECT_CHOICE_COLOR = $80ffff;
  INCORRECT_CHOICE_COLOR = $ffffff;

{ TChoiceEditorFrame }

procedure TChoiceEditorFrame.cbCorrectClick(Sender: TObject);
var
  Input: ISuperObject;
begin
  Input := SO;
  Input.B['correct'] := cbCorrect.Checked;
  Dispatcher.PerformAction(Id, 'setCorrect', Input);
end;

procedure TChoiceEditorFrame.cbCorrectEnter(Sender: TObject);
begin
  SendMessage((Sender as TWinControl).Handle, WM_CHANGEUISTATE, MAKELONG(UIS_SET, UISF_HIDEFOCUS), 0);
end;

procedure TChoiceEditorFrame.AfterConstruction;
var
  Size: TSize;
begin
  inherited;
  FPadEditor := TPadEditorFrame.Create(Shape.S['padEditor'], Dispatcher, Self);
  FPadEditor.BorderSpacing.Top := 2;
  FPadEditor.Align := alClient;

  LoadPngResourceToPicture('ChoiceEditor/fixedChoice.png', imgFixed.Picture);
  LoadPngResourceToPicture('ChoiceEditor/negativeChoice.png', imgNegative.Picture);
  Size := GetPreferredControlSize(pnlFlags);
  pnlLeft.Constraints.MinWidth := Size.cx;
  pnlFlags.Constraints.MinHeight := Size.cy;
end;

destructor TChoiceEditorFrame.Destroy;
begin
  FreeAndNil(FPadEditor);
  inherited;
end;

procedure TChoiceEditorFrame.Render;
var
  Correct: Boolean;
begin
  UpdateShape;
  FPadEditor.Render;

  Correct := Shape.B['correct'];
  SetCheckBoxStateWithoutOnClick(cbCorrect, Correct);
  if Correct then
    FPadEditor.PadColor := CORRECT_CHOICE_COLOR
  else
    FPadEditor.PadColor := INCORRECT_CHOICE_COLOR;

  imgFixed.Visible := Fixed;
  imgNegative.Visible := Shape.B['negative'];
end;

function TChoiceEditorFrame.Fixed: Boolean;
begin
  Result := Shape.B['fixed'];
end;

procedure TChoiceEditorFrame.SetFixed(Value: Boolean);
var
  Input: ISuperObject;
begin
  Input := SO;
  Input.B['fixed'] := Value;
  Dispatcher.PerformAction(Id, 'setFixed', Input);
end;

end.

